package com.navigationtest.tool;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.kunminx.architecture.ui.callback.UnPeekLiveData;
import com.myatest.R;

import androidx.databinding.BindingAdapter;
import androidx.viewpager2.widget.ViewPager2;

/**
 * User: psn
 * Date: 2021/7/22
 * xml示例
          <com.google.android.material.tabs.TabLayout
              android:id="@+id/main_tablelayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintTop_toBottomOf="@+id/title"
              android:background="@color/white"
              app:tabSelectedTextColor="@color/tab_text_1" // 选中后字体颜色
              app:tabIndicatorColor="@color/tab_text_1" //下标颜色
              app:tabTextColor="@color/tab_text_2" // 字体颜色
              app:tabMaxWidth="200dp"
              app:tabMinWidth="20dp"
              />
          <androidx.viewpager2.widget.ViewPager2
              android:id="@+id/main_viewpage"
               setAdapter="@{adapt}" // 绑定 extends FragmentStateAdapter
               setUserInputEnabled="@{true}"   // 设置滑动
               tabLayoutMediator="@{@id/main_tablelayout}" //  绑定TabLayout
              android:layout_width="match_parent"
              android:layout_height="0dp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintTop_toBottomOf="@id/main_tablelayout" />
 */
public class ViewPageBindingAdapter {
    /**
     * 设置viewpage滑动监听
     *
     * @param viewPager2
     * @param pagePosition 观测可获得当前选中的界面
     */
    @BindingAdapter(value = {"registerOnPageChangeCallback"}, requireAll = false)
    public static void registerOnPageChangeCallback(ViewPager2 viewPager2,
                                                    UnPeekLiveData<Integer> pagePosition) {
        //当页面在滑动的时候会调用此方法
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            //此方法是页面跳转完后得到调用，arg0是你当前选中的页面的Position
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }

            //此方法是在状态改变的时候调用，其中arg0这个参数
            //有三种状态（0，1，2）。arg0 ==1的时辰默示正在滑动，arg0==2的时辰默示滑动完毕了，arg0==0的时辰默示什么都没做。
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    /**
     * 关联tablayout与ViewPager2并设置tablayout样式
     *
     * @param viewpage
     * @param tableid
     */
    @BindingAdapter(value = {"tabLayoutMediator"}, requireAll = false)
    public static void tabLayoutMediator(ViewPager2 viewpage, int tableid) {
        TabLayout table = (viewpage.getRootView()).findViewById(tableid);
        table.setTabMode(TabLayout.MODE_FIXED       );//同时显示选项卡，宽度相等
        table.setTabGravity(TabLayout.GRAVITY_CENTER  );

        //绘制分割线
//        LinearLayout linearLayout = (LinearLayout) table.getChildAt(0);
//        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
//        linearLayout.setDividerDrawable(table.getResources().getDrawable(R.drawable.layout_divider_vertical, null));

        new TabLayoutMediator(table, viewpage, true, (tab, position) -> {
            View view =
                    LayoutInflater.from(table.getContext()).inflate(R.layout.task_tab_item,
                            null);

            TextView txt_title = view.findViewById(R.id.tab_txt);
            //设置标签名
            switch (position) {
                case 0:
                    txt_title.setText( "一页");

                    break;
                case 1:
                    txt_title.setText( "二页");
                    break;
                case 2:
                    txt_title.setText( "三页");
                    break;
                default:
                    break;

            }
            tab.setCustomView(view);
            table.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    ((TextView) tab.getCustomView().findViewById(R.id.tab_txt)).
                            setTextColor(view.getResources().getColor(R.color.colorAccent, null));
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                    ((TextView) tab.getCustomView().findViewById(R.id.tab_txt))
                            .setTextColor(view.getResources().getColor(R.color.black, null));

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });



        }
        ).attach();
        //     table.getTabAt(1).select();//标签默认位置
    }





}
